<?php

if (!isset($_REQUEST['client']) || !is_numeric($_REQUEST['client'])) {
    error_obj::setError(new error_obj("bad request client", __FILE__, 'line 4 -> redirect'), UNEXCEPTED);
    base_obj::redirect(ROOT);
}
$clientId = (int) $_REQUEST['client'];
$user_obj = new user_obj();
$client_obj = new client_obj();

if (isset($_REQUEST['action'])) {
    switch ($_REQUEST['action']) {
        case 'edit_client':
            $client = $_REQUEST['client_data'];
            $client['monthlyFee'] = str_replace(',', '.', $client['monthlyFee']);
            if (substr($client['domain'], 0, 1) == '@'){
                $client['domain'] = substr($client['domain'], 1);
            }
            $result = $client_obj->editClient($client, $clientId);
            if (error_obj::isError($result)) {
                error_obj::setError($result, UNEXCEPTED);
            } else {
                error_obj::setGood('Client was updated!');
                base_obj::redirect(ROOT . '?input=list_of_clients');
            }

            break;
    }
}

$client_data = $client_obj->getClient($clientId);

if (error_obj::isError($client_data)) {
    error_obj::setError($client_data, UNEXCEPTED);
} elseif (empty($client_data)) {
    error_obj::setError(new error_obj("empty client", __FILE__, 'get client data'), EMPTY_CLIENT);
} else {
    $filter = array();
    $filter['active'] = 1;
    $filter['client'] = 11;
    $client['contactPersons'] = $user_obj->getUsers('name, surname, users_id', $filter);
    $client['typesOfCoop'] = $client_obj->getClientTypeOfCooperations();

    $GLOBALS['template_data'] = $client;
    $GLOBALS['template_data']['client_data'] = $client_data;
}
?>
